﻿@{
    ViewBag.Title = "Sweet Survey";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="header" class="ui-widget-header ui-corner-top">
    <div id="title"><h1>Sweet Survey</h1></div>

    <div id="logindisplay">
        @Html.Partial("_LogOnPartial")
        @Html.Partial("_ThemePartial")
    </div>

    <div id="menucontainer">
        <input type="radio" id="create" name="menu" value="@Url.Action("Index", "Home")" /><label for="create">Create a Survey</label>
        <input type="radio" id="manage" name="menu" value="@Url.Action("MySurveys", "Home")" disabled="disabled" /><label for="manage">Manage My Surveys</label>
        <input type="radio" id="about" name="menu" value="@Url.Action("About", "Home")" /><label for="about">About Us</label>
    </div>
</div>

<div id="main" class="ui-widget-content ui-corner-bottom">@RenderBody()</div>

<span class="admin"><a href="javascript:admin();">Administrative Portal &gt;&gt;</a></span>

<script type="text/javascript">
    (function () {
        $(function () {
            $('#menucontainer input#create').button({ icons: { primary: "ui-icon-plusthick"} });
            $('#menucontainer input#about').button({ icons: { primary: "ui-icon-tag"} });
            $('#menucontainer input').button();
            $('#menucontainer').change(function () { document.location = $(this).children('input:checked').val(); });
            var menuItemManage = $('#menucontainer input#manage');
            if ($globalVars.isAuthenticated) {
                menuItemManage.button('enable');
            }
            $(document.body).on('LogOn', function () {
                menuItemManage.button('enable');
            });
            $(document.body).on('LogOff', function () {
                menuItemManage.button('disable');
            });
        });
    })();
    function admin() {
        $.post('@Url.Action("AdministratorExists", "Account")', {}, function (response) {
            if (response == false) {
                $.post('@Url.Action("CreateAdministrator", "Account")', {}, function (response) {
                    window.location = '@Url.Action("Index", "Administrator", new { newlyCreated = "true" })';
                });
            }
            else {
                var adminUserName = '@SweetSurvey.Models.AccountMembershipService.AdministratorUserName';
                var adminPortal = '@Url.Action("Index", "Administrator", new { newlyCreated = "true" })';
                if ($globalVars.isAuthenticated && $globalVars.userName == adminUserName) {
                    window.location = adminPortal;
                }
                else if ($globalVars.isAuthenticated && $globalVars.userName != adminUserName) {
                    $('#logOff').trigger('click');
                    setTimeout(function () { $('#logOn').trigger('click'); }, 200);
                }
                else {
                    $('#logOn').trigger('click');
                }
            }
        }, 'json');
    }
</script>